<?php
class FileWidget extends CInputWidget
{
    public $emptyText = 'Select files';
    public $thumbWidth = 48;
    public $thumbHeight = 48;

    public function run()
    {
        //$this->htmlOptions['id'] = $this->getId();
        if (isset($this->htmlOptions['class'])) {
            $this->htmlOptions['class'] .= ' file';
        }
        else {
            $this->htmlOptions['class'] = 'file';
        }
        echo CHtml::openTag('div', $this->htmlOptions);
        $text = $this->emptyText;
        if (is_object($this->model)) {
            echo CHtml::activeHiddenField($this->model, $this->attribute);
            $this->attribute = preg_replace('/\[\d+\]/', '', $this->attribute);
            $value = CHtml::resolveValue($this->model, $this->attribute);
            if (!empty($value)) {
                $text = $value;
            }
        }
        else {
            echo CHtml::hiddenField($this->name, $this->value);
            if (!empty($this->value)) {
                $text = $this->value;
            }
        }

        if (strpos($text, '.') !== false) {
            $ext = substr(strrchr($text, '.'), 1);
            if (in_array(strtolower($ext), array('jpg', 'gif', 'png', 'jpeg'))) {
                $text = CHtml::image($text, $text, array('width' => $this->thumbWidth, 'height' => $this->thumbHeight));
            }
        }

        echo CHtml::link($text, '#', array('class' => 'select-file'));
        echo CHtml::link('x', '#', array('class' => 'deselect-file', 'style' => $text == $this->emptyText ? 'display:none;' : ''));
        echo CHtml::closeTag('div');
        $this->registerScripts();
    }

    public function registerScripts()
    {
        $params = array('SID' => 'Core.FileManager.cmdConnect');
        $urlManager = new CUrlManager;
        if (empty($this->root) === false)
            $params['root'] = $this->root;
        elseif (isset($this->htmlOptions['location']))
            $params['root'] = $this->htmlOptions['location'];
        $connectorUrl = $urlManager->createUrl('/Core/service/command', $params);
        $script = "
jQuery.current_file = null;
jQuery('.select-file').live('click', function(){
    jQuery.current_file = this;
    if(jQuery('#finder').length == 0) {
        jQuery('body').append(jQuery('<div/>').attr('id', 'finder'));
        jQuery('#finder').elfinder({ zIndex: 9999999, places: 'File Manager', url : '" . $connectorUrl . "',
            lang : 'en',
            dialog : { width : 900, modal : true, title : 'File Manager', zIndex: 7777777 },
            editorCallback : function(url) {
                var text = url;
                url = url.replace('" . Yii::app()->request->hostInfo . "', '');
                if (/(\.gif|\.jpg|\.png|\.jpeg)/i.test(text))
                {
                    text = jQuery('<img width=\"{$this->thumbWidth}\" height=\"{$this->thumbHeight}\" />').attr('src', text).attr('alt', text);
                }
                else
                {
                    text = decodeURIComponent(url);
                }
                jQuery(jQuery.current_file).html(text)
                    .prev('input').val(url)
                    .end()
                    .next('.deselect-file').show();
            },
            closeOnEditorCallback: true
        });
        }
    else {
        jQuery('#finder').elfinder('open');
    }
    return false;
});
jQuery('.deselect-file').live('click', function(){
    jQuery(this).hide()
        .prev('.select-file').text('" . $this->emptyText . "')
        .end()
        .siblings('input').val('');
    return false;
});
";
        Yii::app()->clientScript->registerScript(__CLASS__ . '#FileManage', $script);
        $asset = Yii::app()->core->AssetUrl . '/elfinder-1.1';
        $cs = Yii::app()->clientScript;
        $cs->registerCoreScript('jquery.ui');
        $cs->registerScriptFile($asset . '/js/elfinder.min.js', CClientScript::POS_BEGIN);
        $cs->registerCssFile($asset . '/css/elfinder.css');
        $cs->registerCssFile(Yii::app()->theme->baseUrl . '/styles/widgets/jquery.ui.dialog.css');
    }
}